package com.wzdigit.wms.wms.service.order;

import com.wzdigit.wms.basic.domain.dtos.trsn.TrsnDtlDto;
import com.wzdigit.wms.basic.domain.dtos.trsn.TrsnDto;
import com.wzdigit.wms.basic.domain.order.TrsnDtl;
import com.wzdigit.wms.basic.domain.order.TrsnDtlGoods;
import com.wzdigit.wms.basic.domain.order.TrsnHeader;
import com.wzdigit.wms.wms.service.core.trsn.TrsnDtlGoodsService;
import com.wzdigit.wms.wms.service.core.trsn.TrsnDtlService;
import com.wzdigit.wms.wms.service.core.trsn.TrsnHeaderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 * 库存交易凭证 服务类
 * </p>
 *
 * @author
 * @since 2021-01-12
 */
@Service
@Slf4j
public class TrsnService {

    @Autowired
    @Lazy
    TrsnHeaderService trsnHeaderService;

    @Autowired
    @Lazy
    TrsnDtlService trsnDtlService;

    @Autowired
    @Lazy
    TrsnDtlGoodsService trsnDtlGoodsService;


    /**
     * 生成出入库凭证
     * 生成出入库凭证、出入库凭证明细、出入库凭证明细货品清单(可选)
     *
     * @param trsnDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public TrsnDto addNewOne(TrsnDto trsnDto) {

        //【新增】库存交易凭证表头
        TrsnHeader trsnHeader = trsnDto.getTrsnHeader();
        trsnHeader = trsnHeaderService.addNewOne(trsnHeader);


        //【新增】库存交易凭证明细,货品清单
        final Integer trsnHeaderId = trsnHeader.getTrsnHeaderId();//表头ID

        List<TrsnDtlDto> trsnDtlDtoList = trsnDto.getTrsnDtlDtoList();

//        for (TrsnDtlRequest trsnDtlRequest : trsnDtlRequestList) {
        for (TrsnDtlDto trsnDtlDto : trsnDtlDtoList) {


            //设置表头ID
            TrsnDtl trsnDtl = trsnDtlDto.getTrsnDtl();
            trsnDtl.setTrsnHeaderId(trsnHeaderId);
            //新增明细
            trsnDtl = trsnDtlService.addNewOne(trsnDtl);
            trsnDtlDto.setTrsnDtl(trsnDtl);

            //新增货品清单
            List<TrsnDtlGoods> dtoTrsnDtlGoodsList = trsnDtlDto.getTrsnDtlGoodsList();

            //循环设置明细ID
            for (TrsnDtlGoods trsnDtlGoods : dtoTrsnDtlGoodsList) {
                trsnDtlGoods.setTrsnDtlId(trsnDtl.getTrsnDtlId());
            }

            if (dtoTrsnDtlGoodsList != null && dtoTrsnDtlGoodsList.size() > 0) {
                //新增【库存交易凭证明细货品清单】
                dtoTrsnDtlGoodsList = trsnDtlGoodsService.addNewList(dtoTrsnDtlGoodsList);
                trsnDtlDto.setTrsnDtlGoodsList(dtoTrsnDtlGoodsList);
            }
        }

        //设置回参
        trsnDto.setTrsnHeader(trsnHeader).setTrsnDtlDtoList(trsnDtlDtoList);

//        Result<TrsnRequest> result = new Result<>();
//        result.setData(trsnRequest);
        return trsnDto;
    }


    /**
     * 更新出入库凭证
     * 更新出入库凭证、出入库凭证明细、出入库凭证明细货品清单(可选)
     *
     * @param trsnDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
//    public Result<TrsnRequest> updateOne(TrsnRequest trsnRequest) {
    public TrsnDto updateOne(TrsnDto trsnDto) {

        //库存交易凭证表头
        TrsnHeader trsnHeader = trsnDto.getTrsnHeader();
        trsnHeader = trsnHeaderService.updateOne(trsnHeader);

        //库存交易凭证明细
        List<TrsnDtlDto> trsnDtlDtoList = trsnDto.getTrsnDtlDtoList();


        for (TrsnDtlDto trsnDtlDto : trsnDtlDtoList) {
            //更新明细
            TrsnDtl trsnDtl = trsnDtlDto.getTrsnDtl();
            trsnDtl = trsnDtlService.updateOne(trsnDtl);
            trsnDtlDto.setTrsnDtl(trsnDtl);

            List<TrsnDtlGoods> dtoTrsnDtlGoodsList = trsnDtlDto.getTrsnDtlGoodsList();

            if (dtoTrsnDtlGoodsList != null && dtoTrsnDtlGoodsList.size() > 0) {
//                List<TrsnDtlGoods> trsnDtlGoodsList = dtoTrsnDtlGoodsList;
                dtoTrsnDtlGoodsList = trsnDtlGoodsService.updateList(dtoTrsnDtlGoodsList);
                trsnDtlDto.setTrsnDtlGoodsList(dtoTrsnDtlGoodsList);
            }
        }
        //设置回参
        trsnDto.setTrsnHeader(trsnHeader).setTrsnDtlDtoList(trsnDtlDtoList);

        return trsnDto;
    }

}
